package com.itestor.iptv.modules.common.actions;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import com.itestor.iptv.modules.common.vo.User;
import com.itestor.iptv.modules.ne.service.RobotManageService;

/**
 * 
 * @author Mr.li
 * 
 */
public class SystemStatusAction extends DispatchAction {

    /**
     * Logger for this class.
     */
    private static final Logger logger = Logger
            .getLogger(SystemStatusAction.class);

    private RobotManageService robotManageService;
    
    private static final DateFormat DATE_FAMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",
            Locale.UK);
    
    public static Map<String, String> startTimes = new HashMap<String, String>();
    
    static {
        startTimes.put("sysuptime" , DATE_FAMAT.format(new Date()));
    }

    /**
     * 
     * @param pRegionManageService
     */
    public void setRobotManageService(RobotManageService pRobotManageService) {
        robotManageService = pRobotManageService;
    }

    /**
     * 
     * @param pMapping
     * @param pForm
     * @param pRequest
     * @param pResponse
     * @return ActionForward
     * @throws Exception
     */
    public ActionForward querySystemDetail(ActionMapping pMapping,
            ActionForm pForm, HttpServletRequest pRequest,
            HttpServletResponse pResponse) throws Exception {
        
        int nodeCount = robotManageService.queryAllRobotManagers();
        int userCount = User.getNum();
        Date currentTime = new Date();
        String startTime = startTimes.get("sysuptime");
        
        pRequest.setAttribute("nodeCount", nodeCount);
        pRequest.setAttribute("userCount", userCount);
        pRequest.setAttribute("currentSysTime", DATE_FAMAT.format(currentTime));
        pRequest.setAttribute("systemUpTime", (startTime==null)?"":startTime);
        
        return pMapping.findForward("FW.SUCCESS");
    }

}